package third.lib.utils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class XMLUtils {
    public static Map<String, Object> prase(String xmlStr) {
        HashMap map = new HashMap();

        try {
            Document document = DocumentHelper.parseText(xmlStr);
            Element rootElement = document.getRootElement();
            prase(map, rootElement);
            return map;
        } catch (DocumentException var4) {
            var4.printStackTrace();
            return null;
        }
    }

    private static void prase(Map<String, Object> map, Element element) {
        short type = element.getNodeType();
        if (type == 1) {
            if (element.elements().size() == 0) {
                map.put(element.getName(), element.getStringValue());
            } else {
                Map<String, Object> nMap = new HashMap();
                map.put(element.getName(), nMap);
                Iterator iterator = element.elementIterator();

                while(iterator.hasNext()) {
                    Element nElement = (Element)iterator.next();
                    prase(nMap, nElement);
                }
            }
        }

    }
}
